#include "messagepack.h"
#include "messagepackettest.h"

#include <QtTest>

void MessagePackTest::marshalTest_data()
{
	QTest::addColumn<QString>("data");
	QTest::addColumn<int>("intData");
	QTest::addColumn<long long>("longlongData");
	QTest::addColumn<QString>("stringData");

	QTest::newRow("Only int")<<"i"<<23<<(long long)0<<"";
	QTest::newRow("only long long")<<"q"<<0<<(long long)111111111<<"";
	QTest::newRow("allData")<<"iqs"<<13123<<(long long)1115555888<<"Hello world";
}

void MessagePackTest::marshalTest()
{
	QFETCH(QString,data);
	QFETCH(int, intData);
	QFETCH(long long, longlongData);
	QFETCH(QString, stringData);

	boost::shared_ptr<MessagePack> messagePack = MessagePack::createMessagePack();

	if(data.contains("i"))
	{
		QVERIFY(messagePack->appendData("intData", intData));
	}
	if(data.contains("q"))
	{
		QVERIFY(messagePack->appendData("longlongData", longlongData));
	}
	if(data.contains("s"))
	{
		QVERIFY(messagePack->appendData("stringData", stringData));
	}

//	QVERIFY(!messagePack.appendData("wrongData", QByteArray()));

	QByteArray result = messagePack->marshal();
	qDebug()<<result;
}

void MessagePackTest::marshalAndUnmarshalTest()
{
	int a = 10, b = 100;
	long long fileLength = 512000;
	QString fileName = "test.txt";

	boost::shared_ptr<MessagePack> messagePack = MessagePack::createMessagePack();
	QVERIFY( messagePack->appendData("a", a) );
	QVERIFY( messagePack->appendData("b", b) );
	QVERIFY( messagePack->appendData("fileLength", fileLength) );
	QVERIFY( messagePack->appendData("fileName", fileName) );

	QByteArray marshalResult = messagePack->marshal();

	boost::shared_ptr<MessagePack> otherMess = MessagePack::createMessagePack();
	QTEST_ASSERT( otherMess->unmarshal(marshalResult) );

	int ta,tb;
	long long tFileLength;
	QString tFileName;

	QVERIFY(otherMess->getData("a", ta));
	QVERIFY(ta == a);

	QVERIFY(otherMess->getData("b", tb));
	QVERIFY(tb == b);

	QVERIFY(otherMess->getData("fileLength", tFileLength));
	QVERIFY(tFileLength == fileLength);

	QVERIFY(otherMess->getData("fileName", tFileName) );
	QVERIFY(tFileName == fileName);

	QVERIFY( !otherMess->getData("fileName", ta) );
	QVERIFY( !otherMess->getData("notexits", ta));

}
